define(["angularAMD"],function(angularAMD){
    angularAMD.service('CheckInfoService',['$state','$rootScope', '$filter', '$stateParams', 'config', '$mdDialog', 'MMWService', '$calculater',
        function($state, $rootScope, $filter, $stateParams, config, $mdDialog, $MMWService, $calculater){
            this.init = function(options){
                var picLimitNum = 5;
                function qcformMeasuredataAttachfile(info) {
                    var content = {
                        qcform_no : options.qc_form_no,
                        qc_type:options.qc_type,
                        item_no:options.item_no,
                        image_detail: [],
                    };
                    var picSrc=[];
                    for(var i=0;i<info.picSrcArray.length;i++){
                        picSrc[i] = info.picSrcArray[i];
                        content.image_detail.push({
                            image_src:picSrc[i],
                            image_seq:i,
                            image_format:'jpg'
                        });
                    }
                    $MMWService.sendToSMESServer({
                        uri: 'tcSPC.clsSPC.qcform_measuredata_attachfile',
                        content: content,
                        success: function () {
                            $rootScope.showToast($filter('translate')('check_info.msg.success'));
                            info.hide();
                            if(options.feedback)
                                options.feedback();
                        }
                    });
                }

                var openDialog = function(options){
                    $mdDialog.dialog(
                        'module/modulesSPC/program/B0503/checkInfo.tmp.html',                    
                        function(dialog){
                            return {
                                css_url:'module/modulesSPC/program/B0503/checkInfo.css',
                                // server_url:'http://'+config.server.ip+':'+config.server.port,
                                back : function(){
                                    if(options.close){
                                        options.close(dialog);
                                    } else {
                                        dialog.hide();
                                    }
                                },
                                confirm: function () {
                                    msg = '';
                                    if(msg == ''){
                                        qcformMeasuredataAttachfile(dialog);
                                    } else {
                                        $rootScope.showAlert(msg);
                                    }
                                },
                                picSrcArray : [],
                                //拍照
                                pic_add : function(){
                                    if(dialog.picSrcArray.length < picLimitNum){
                                        if(typeof(navigator.camera) !== 'undefined'){
                                            //回傳URI
                                            navigator.camera.getPicture(
                                                function(imageURI) {
                                                    
                                                if($rootScope.compressImage)
                                                {
                                                    $rootScope.compressImage(imageURI, 
                                                        function(data){  
                                                            dialog.picSrcArray.push(data);
                                                            $rootScope.$apply();
                                                            }); 
                                                }
                                                else
                                                {
                                                    dialog.picSrcArray.push(imageURI);
                                                    $rootScope.$apply();
                                                }
                                                    //$scope.data.scrollLeft = document.getElementById('img-tile-list').clientWidth;
                                                },
                                                function(message) {
                                                    //alert('Failed because: ' + message);
                                                },
                                                {
                                                    quality: 50,
                                                    destinationType: Camera.DestinationType.DATA_URL
                                                }
                                            );
                                        } else if(window.nodeRequire){
                                            //回傳URI
                                            try {
                                                var remote = window.nodeRequire('electron').remote;
                                                remote.dialog.showOpenDialog({
                                                    properties: ['openFile', 'multiSelections'],
                                                    filters: [
                                                        {name: 'Images', extensions: ['jpg', 'png', 'gif', 'bmp']}
                                                    ]
                                                },function(fileList){
                                                    $rootScope.showLoading();
                                                    $rootScope.$apply();
                                                    setTimeout(function(){
                                                        if(fileList){
                                                            if(fileList.length == 0){
                                                                //
                                                            } else if((dialog.picSrcArray.length + fileList.length) > picLimitNum){
                                                                $rootScope.showAlert($filter('translate')('check_info.msg.pic_limit', {num:picLimitNum}));
                                                            } else {
                                                                var fs = window.nodeRequire('fs');
                                                                fileList.forEach(function(fileName){
                                                                    var image = fs.readFileSync(fileName);
                                                                    if(image)
                                                                    {
                                                                        
                                                                    if($rootScope.compressImage)
                                                                    {
                                                                        $rootScope.compressImage(new Buffer(image).toString('base64'), 
                                                                        function(data){dialog.picSrcArray.push(data)}); 
                                                                    }
                                                                    else
                                                                    {
                                                                        dialog.picSrcArray.push(new Buffer(image).toString('base64'));
                                                                    }
                                                                    }

                                                                });
                                                            }
                                                        }
                                                        $rootScope.hideLoading();
                                                        $rootScope.$apply();
                                                    });
                                                });
                                            } catch (error) {
                                                $('body').append('<input id="fileDialog" type="file" accept=".jpg,.png,.gif,.bmp" multiple size="5" >');
                                                var file_field = $('#fileDialog');
                                                file_field.on('change', function(){
                                                    if(file_field[0].files && file_field[0].files.length>0){
                                                        var fileList = [];
                                                        for(var i =0;i<file_field[0].files.length;i++){
                                                            fileList.push(file_field[0].files[i].path);
                                                        }
                                                        if((dialog.picSrcArray.length + fileList.length) > picLimitNum){
                                                            $rootScope.showAlert($filter('translate')('B0207.msg.pic_limit', {num:picLimitNum}));
                                                        } else {
                                                            var fs = window.nodeRequire('fs');
                                                            fileList.forEach(function(fileName){
                                                                var image = fs.readFileSync(fileName);
                                                                if(image)
                                                                    dialog.picSrcArray.push(new Buffer(image).toString('base64'));
                                                            });
                                                        }
                                                    }
                                                    file_field.remove();
                                                });
                                                file_field.click();
                                            }

                   
                                        } else {
                                            $rootScope.showAlert($filter('translate')('common.msg.cant_use_camera'));
                                        }
                                    } else {
                                        $rootScope.showAlert($filter('translate')('check_info.msg.pic_limit', {num:picLimitNum}));
                                    }
                                },
                                pic_delete : function(index){
                                    dialog.picSrcArray.splice(index,1);
                                },
                                pic_open : function(index){
                                    $gallery.show('#'+(index+1), 'data:image/jpeg;base64,'+dialog.picSrcArray[index], function(){

                                    });
                                },
                                open_checkPic: function(item){
                                    $gallery.show(item.reason_no, dialog.server_url+item.picture_path, function(){});
                                },
                            }
                        }
                    );
                }

                openDialog(options);
            };   
        }
    ])
});